package com.example.work2;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class DatabaseActivity extends AppCompatActivity {

    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_database);

        button1=findViewById(R.id.button11);
        button2=findViewById(R.id.button12);
        button3=findViewById(R.id.button13);
        button4=findViewById(R.id.button14);

        MyDBHelper myDBHelper=new MyDBHelper(this,"student",null,1);
        SQLiteDatabase database=myDBHelper.getWritableDatabase();

        //insert
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ContentValues values1=new ContentValues();
                values1.put("name","humingwei");
                values1.put("age",20);

                ContentValues values2=new ContentValues();
                values2.put("name","panlingfneg");
                values2.put("age",21);

                ContentValues values3=new ContentValues();
                values3.put("name","xiaohu");
                values3.put("age",25);

                database.insert("student",null,values1);
                database.insert("student",null,values2);
                database.insert("student",null,values3);
            }
        });

        //update
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ContentValues values=new ContentValues();
                values.put("age",19);
                database.update("student",values,"name=?",new String[]{"xiaohu"});
            }
        });

        //delete
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                database.execSQL("delete from student");
             // database.delete("student","name=?",new String[]{"xiaohu"});
            }
        });

        //query
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Cursor cursor1=database.query("student",new String[]{"name"},
                        "name=?",new String[]{"xiaohu"},null,null,null);

                Cursor cursor=database.rawQuery("select * from student where name=?",new String[]{"humingwei"});
//                database.query(String.)

                Log.d("hmw",String.valueOf(cursor.getCount()));

                while (cursor.moveToNext()){
                    @SuppressLint("Range") Integer id = cursor.getInt(cursor.getColumnIndex("id"));
                    @SuppressLint("Range") Integer age = cursor.getInt(cursor.getColumnIndex("age"));
                    @SuppressLint("Range") String name=cursor.getString(cursor.getColumnIndex("name"));
                    Log.d("hmw","result：" +"id是"+id+" age是"+age +" name是"+ name);
                }
                cursor.close();
            }
        });
    }
}